package com.qc.dao.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.qc.common.StringUtil;
import com.qc.dao.OccupationDao;
import com.qc.entity.OccupationEntity;

@Repository("occupationDao")
public class OccupationDaoImpl implements OccupationDao{

    @Resource(name = "jdbcTemplate")
    private JdbcTemplate jdbcTemplate;
    
    @Override
    public int getListOccupationCount(String name) {
        
        String sql = "select count(*) from t_occupation where 1 = 1 and if_del = '0' ";
        if (StringUtil.isNotEmpty(name) && !"undefined".equals(name) ) {
            sql += " and name like '%"+name+"%' ";
        }
        return jdbcTemplate.queryForInt(sql);
    }

    @Override
    public List<Map<String, Object>> getAllOccupation(String name, int pageNo,int pageSize) {

        int beginRow = (pageNo - 1)* pageSize;
        String sql = "select id,name,price from t_occupation where 1 = 1 and if_del = '0'  ";
        
        if (StringUtil.isNotEmpty(name) && !"undefined".equals(name) ) {
            sql += " and name like '%"+name+"%' ";
        }
        
        sql += " order by created_time DESC ";
        sql += " LIMIT "+ String.valueOf(beginRow)+ "," + pageSize;
        
        return jdbcTemplate.queryForList(sql);
    }

    @Override
    public int saveOccupation(OccupationEntity occupationEntity) {

        String sql = "insert into t_occupation(name,price) values(?,?)";
        return jdbcTemplate.update(sql,new Object[]{occupationEntity.getName(),occupationEntity.getPrice()});
    }

    @Override
    public int deleteOccupationId(long id) {

        String sql = "update t_occupation set if_del = '1' where id = " +id;
        return jdbcTemplate.update(sql);
    }

    @Override
    public int updateOccupationId(OccupationEntity occupationEntity) {

        String sql = "update t_occupation set name = ? ,price=?  where id = ? ";
        return jdbcTemplate.update(sql,new Object[]{occupationEntity.getName(),occupationEntity.getPrice(),occupationEntity.getId()});
    }

}
